package paneles;

import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import proy_si.ConexionBD;

public class MainJFrame extends JFrame {

	private static final long serialVersionUID = -8346289335641046195L;
	private JPanel contentPane;
	private final JPanel panel = new JPanel();
	private final JPanel panel_1 = new panelBusqueda();
	private final JPanel panel_2 = new panelMapaImagen();
	private final JPanel panel_5;
	private final JPanel panel_6 = new JPanel();
	private final JPanel panel_7 = new PanelGraficos();
	private final JPanel panel_8 = new panelAnuncio();
	private final JButton btnAtras = new JButton("< Atr\u00E1s");
	private final JButton btnIniciarSesion = new JButton("Iniciar sesi\u00F3n");
	private final JButton btnCerrarSesion = new JButton("Cerrar sesi\u00F3n");
	private final JButton btnAgregarVivienda = new JButton("Agregar vivienda");
	private final JButton btnGraficos = new JButton("Gr\u00E1ficos y estad\u00EDsticas");
	private final JMenuBar menuBar = new JMenuBar();
	private final JButton btnPlanCuadrante = new JButton("Plan cuadrante");

	/**
	 * Create the frame.
	 */
	public MainJFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1024, 768);
		menuBar.setFont(new Font("Segoe UI", Font.PLAIN, 10));
		
		setJMenuBar(menuBar);
		btnGraficos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mostrarGrafico();
			}
		});
		
	
		
		btnGraficos.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		menuBar.add(btnGraficos);
		btnAgregarVivienda.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl = (CardLayout)(contentPane.getLayout());
				cl.show(contentPane, "panel 5");
				btnIniciarSesion.setVisible(false);
				btnAtras.setVisible(true);
				btnGraficos.setVisible(false);		
				btnAgregarVivienda.setVisible(false);
				btnCerrarSesion.setVisible(false);
				btnPlanCuadrante.setVisible(false);
			}
		});
		btnPlanCuadrante.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		
		menuBar.add(btnPlanCuadrante);
		btnAgregarVivienda.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		menuBar.add(btnAgregarVivienda);
		btnAtras.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		menuBar.add(btnAtras);
		btnIniciarSesion.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		menuBar.add(btnIniciarSesion);
		btnCerrarSesion.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		menuBar.add(btnCerrarSesion);
		btnCerrarSesion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(panelLoginUser.getSesion() != null){
					panelLoginUser.setSesion(false);
					JOptionPane.showMessageDialog(null,"Ha salido de su sesion");
					btnCerrarSesion.setVisible(false);
					btnIniciarSesion.setVisible(true);
					btnAgregarVivienda.setVisible(false);					
				}
			}
		});
		btnCerrarSesion.setVisible(false);
		btnIniciarSesion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl = (CardLayout)contentPane.getLayout();
				cl.show(contentPane, "panel 2");
				btnIniciarSesion.setVisible(false);
				btnAtras.setVisible(true);
				btnAgregarVivienda.setVisible(false);
				btnGraficos.setVisible(false);
				btnPlanCuadrante.setVisible(false);
			}
		});
		btnAtras.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout)(contentPane.getLayout());
				cl.show(contentPane, "panel 1");
				if(panelLoginUser.getSesion()==null){
					btnIniciarSesion.setVisible(true);
				}else{
					btnAgregarVivienda.setVisible(true);
					btnIniciarSesion.setVisible(false);
					btnCerrarSesion.setVisible(true);
				}
				btnAtras.setVisible(false);
				btnGraficos.setVisible(true);
				//btnAgregarVivienda.setVisible(true);
				btnPlanCuadrante.setVisible(true);
			}
		});
		
		btnAtras.setVisible(false);
		btnAgregarVivienda.setVisible(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new CardLayout(0, 0));
		
		contentPane.add(panel, "panel 1");
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0, 0};
		gbl_panel.rowHeights = new int[]{0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(0, 0, 0, 5);
		gbc_panel_1.fill = GridBagConstraints.VERTICAL;
		gbc_panel_1.gridx = 0;
		gbc_panel_1.gridy = 0;
		panel.add(panel_1, gbc_panel_1);
		
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.fill = GridBagConstraints.BOTH;
		gbc_panel_2.gridx = 1;
		gbc_panel_2.gridy = 0;
		panel.add(panel_2, gbc_panel_2);
		
		panel_5 = new panelLoginUser(contentPane, this);
		
		contentPane.add(panel_5, "panel 2");
		
		contentPane.add(panel_6, "panel 3");
		
		contentPane.add(panel_7, "panel 4");
		
		contentPane.add(panel_8, "panel 5");
		btnAgregarVivienda.setVisible(false);
		pack();
	}
	
	private void mostrarGrafico(){
		CardLayout cl = (CardLayout)(contentPane.getLayout());
		cl.show(contentPane, "panel 4");
		btnIniciarSesion.setVisible(false);
		btnAtras.setVisible(true);
		btnGraficos.setVisible(false);
		btnCerrarSesion.setVisible(false);
		btnPlanCuadrante.setVisible(false);
		btnAgregarVivienda.setVisible(false);
	}

	public JButton getBtnIniciarSesion() {
		return btnIniciarSesion;
	}
	public JButton getBtnCerrarSesion() {
		return btnCerrarSesion;
	}
	public JButton getBtnAtras() {
		return btnAtras;
	}
	public JButton getBtnAgregarVivienda() {
		return btnAgregarVivienda;
	}
	public JButton getBtnGraficos() {
		return btnGraficos;
	}

}
